package servlet;

import model.Employee;
import org.thymeleaf.TemplateEngine;
import org.thymeleaf.context.WebContext;
import util.JDBCUtil;
import util.TemplateEngineUtil;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

@WebServlet("/ToUpdateEmployeeServlet")
public class ToUpdateEmployeeServlet extends HttpServlet {
    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

            String employeeId=req.getParameter("employeeId");
            //根据employeeId找到这条员工记录
            String sql="select * from employee where employeeId=?";
            List params=new ArrayList();
            params.add(employeeId);
            ResultSet rs= JDBCUtil.execQuery(sql,params);
            //将待修改的数据赋给employee
            Employee employee=new Employee();
            try {
                if(rs.next())
                {
                    employee.setEmployeeId(rs.getInt("employeeId"));
                    employee.setEmployeeName(rs.getString("employeeName"));
                    employee.setUsername(rs.getString("username"));
                    employee.setEmail(rs.getString("email"));
                    employee.setPhone(rs.getString("phone"));

                }
            }
            catch (Exception e){
                e.printStackTrace();
            }
            //跳转到updateEmployee.html页面
        TemplateEngine engine =TemplateEngineUtil.getTemplateEngine(req.getServletContext());
        WebContext context=new WebContext(req,resp,req.getServletContext());
        context.setVariable("employee",employee);
        engine.process("updateEmployee.html",context,resp.getWriter());

    }
}
